
//导入明文密码处理
const bcrypt = require('bcryptjs')

//注册处理函数
const db = require("../db/index.js")
exports.regUser = (req,res) =>{
  const userinfo = req.body
  if(!userinfo.username || !userinfo.password){
    return res.send({
      status: 1,
      message:'用户名或密码不合法！'
    })
  }
  const sql = 'select * from ev_user where username = ?'

  db.query(sql,[userinfo.username], function(err,results){
    if(err){
      return res.send({
        status:1,
        message:err
      })
    }

    if(results.length > 0){
      return res.send({
        status:1,
        message: '用户被占用，请更换其他用户名！'
      })
    }
    const sql = 'insert into ev_users set ?'
    userinfo.password = bcrypt.hashSync(userinfo.password,10)

    db.query(sql,{username:userinfo.username,password:userinfo.password},(err,results)=>{
      if(err){
        return res.send({
          status:1,
          message:err.message,
        })
      }
      if(results.affectedRows !==1){
        return res.send({
          status:1,
          message:'注册用户失败，请重新注册'
        })
      }

      res.send({
        status:0,
        message:'注册成功'
      })
    })
  })

  res.send('reguser Ok')
}

//登录处理函数
exports.login  = (req,res) =>{
  res.send('login Ok')
}